package xyz.ddlnt.servicemq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import xyz.ddlnt.commonutil.constant.FieldConstants;
import xyz.ddlnt.commonutil.constant.MqConstants;
import xyz.ddlnt.commonutil.constant.StatusConstants;
import xyz.ddlnt.model.dto.UserContentInteractionDTO;
import xyz.ddlnt.model.entity.PrivateMessageList;
import xyz.ddlnt.servicemq.mapper.PrivateMessageListMapper;
import xyz.ddlnt.servicemq.service.PrivateMessageListService;

import java.util.Map;

/**
 * @author ZZULI_SE 210910
 * @data 2025/5/26 19:45
 */
@Service
public class PrivateMessageListServiceImpl extends ServiceImpl<PrivateMessageListMapper, PrivateMessageList> implements PrivateMessageListService {
    @Resource
    private PrivateMessageListMapper privateMessageListMapper;

    @Override
    public void receiverStatusDelete(UserContentInteractionDTO userContentInteractionDTO) {
        // 查询私信对象
        LambdaQueryWrapper<PrivateMessageList> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(wrapper -> wrapper
                .eq(PrivateMessageList::getId, userContentInteractionDTO.getBloggerId())
                .eq(PrivateMessageList::getReceiverId, userContentInteractionDTO.getUserId())
        )
                .select(PrivateMessageList::getId);
        PrivateMessageList privateMessageList = privateMessageListMapper.selectOne(queryWrapper);

        // 如果私信对象存在，则更新私信对象的接收者状态为已删除
        if (privateMessageList != null) {
            LambdaUpdateWrapper<PrivateMessageList> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper
                    .set(PrivateMessageList::getReceiverStatus, StatusConstants.RECEIVER_DELETED)
                    .eq(PrivateMessageList::getId, privateMessageList.getId());
            privateMessageListMapper.update(updateWrapper);
        }
    }

    @Override
    public void unreadMessagesPlusOne(Map<String, String> map) {
        LambdaUpdateWrapper<PrivateMessageList> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.setIncrBy(PrivateMessageList::getUnreadCount, MqConstants.PLUS_ONE)
                .and(wrapper -> wrapper
                        .eq(PrivateMessageList::getUserId, map.get(FieldConstants.RECEIVER_ID))
                        .eq(PrivateMessageList::getReceiverId, map.get(FieldConstants.SENDER_ID)));
        privateMessageListMapper.update(updateWrapper);
    }
}
